package com.mynews.test;

import android.content.Context;
import android.support.v4.util.SparseArrayCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2017/6/28.
 * 对原有的RecycleviewAdapter 进行扩展并且不改变原来的Adapter
 */

public class HeaderAndFooterWrapper extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final String TAG = HeaderAndFooterWrapper.class.getSimpleName();
    private RecyclerView.Adapter innerAdapter;
    private static final int BASE_ITEM_TYPE_HEADER = 100000;//头部的起始数
    private static final int BASE_ITEM_TYPE_FOOTER = 200000;//尾部的起始数

    private Context mContext;

    private SparseArrayCompat<View> mHeaderViews = new SparseArrayCompat<>();
    private SparseArrayCompat<View> mFooterViews = new SparseArrayCompat<>();

    public HeaderAndFooterWrapper(RecyclerView.Adapter adapter,Context context) {
        innerAdapter = adapter;//原有的不包含头部和尾部的 Recycleview adapter
        this.mContext = context;
    }


    public void addHeaderView(View view) {
        mHeaderViews.put(mHeaderViews.size() + BASE_ITEM_TYPE_HEADER, view);
    }

    public void addFooterView(View view) {
        mFooterViews.put(mFooterViews.size() + BASE_ITEM_TYPE_FOOTER, view);
    }

    public int getHeaderViewSize() {
        return mHeaderViews.size();
    }

    public int getFooterViewSize() {
        return mFooterViews.size();
    }

    public View getHeadView(int position) {
        View view = null;
        if (position > getHeaderViewSize() - 1) {
            Log.e(TAG, "getHeadView: 请求的头部view不存在");
            return view;
        }
        return mHeaderViews.get(position);
    }

    public View getFooterView(int position) {
        View view = null;
        if (position > getFooterViewSize() - 1) {
            Log.e(TAG, "getHeadView: 请求的尾部view不存在");
            return view;
        }
        return mFooterViews.get(position);
    }

    public boolean isHeaderView(int position){
        return position < mHeaderViews.size();
    }
    public boolean isFooderView(int position){
        return position >= getHeaderViewSize() + innerAdapter.getItemCount();
    }


    @Override
    public int getItemViewType(int position) {
        if (isHeaderView(position)) {
            return mHeaderViews.keyAt(position);
        } else if (isFooderView(position)) {
            return mFooterViews.keyAt(position - getHeaderViewSize() - innerAdapter.getItemCount());
        } else {
            return innerAdapter.getItemViewType(position - getHeaderViewSize());
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mHeaderViews.get(viewType) != null) {
            View view = mHeaderViews.get(viewType);
            return ViewHolder.createViewHolder(mContext,view);
        }else if(mFooterViews.get(viewType) != null){
            View view = mFooterViews.get(viewType);
            return ViewHolder.createViewHolder(mContext,view);
        }else {
            return innerAdapter.onCreateViewHolder(parent,viewType);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(isHeaderView(position)){
            return;
        }
        if(isFooderView(position)){
            return;
        }
        innerAdapter.onBindViewHolder(holder,position - getHeaderViewSize());
    }

    @Override
    public int getItemCount() {
        return innerAdapter.getItemCount() + mFooterViews.size() + mHeaderViews.size();
    }
}
